package org.springextensions.actionscript.mvcs.service.operation
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import org.springextensions.actionscript.mvcs.command.AbstractLoadCompleteCommand;

	public class LoaderOperation extends FileLoadOperation
	{
		protected var _loader:Loader;
		
		public function get loaderContext():LoaderContext
		{
			return null;
		}
		
		override protected function get bytesLoaded():int 
		{
			return (_loader) ? _loader.contentLoaderInfo.bytesLoaded : 0;
		}
		
		override protected function get bytesTotal():int 
		{
			return (_loader) ? _loader.contentLoaderInfo.bytesTotal : 0;
		}
		
		public function LoaderOperation(loadCompleteAction:AbstractLoadCompleteCommand)
		{
			super(loadCompleteAction);
		}
		
		override public function execute():void
		{
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleError);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, dispatchProgress);
			//_loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, handleError);
			
			var context:LoaderContext = loaderContext;

			_loader.load(new URLRequest(url), loaderContext);
		}
		
		override public function get result():*
		{
			return _loader.contentLoaderInfo.content;
		}
	}
}